package serv

import (
	"context"
	"generate_grom_mode_and_dao/service/genertategrom/api/internal/svc"
	"generate_grom_mode_and_dao/service/genertategrom/api/model"
	"generate_grom_mode_and_dao/service/genertategrom/api/util"
	"github.com/tal-tech/go-zero/core/logx"
	"strings"
)

type GenerateDaoService interface {
	Create(model.Table, *[]model.TableFields) error
}

type defaultGenerateDaoService struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGenerateDaoService(ctx context.Context, svcCtx *svc.ServiceContext) *defaultGenerateDaoService {
	return &defaultGenerateDaoService{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

/**
 *  Create
 *  @Description: 生成dao层文件
 *  @receiver s
 *  @param table
 *  @param fields
 *  @return error
 */
func (s defaultGenerateDaoService) Create(table model.Table, fields *[]model.TableFields) error {
	var err error
	//生成dao文件
	var builderDao strings.Builder
	_, err = builderDao.WriteString("package " + s.svcCtx.Config.GenerateModel.SavePackageName + "\n\n")
	if nil != err {
		return err
	}
	//导入包
	_, err = builderDao.WriteString("import (" + "\n")
	_, err = builderDao.WriteString("\t" + "\"gorm.io/gorm\"" + "\n")
	_, err = builderDao.WriteString(")\n" + "\n")
	//
	funName := "GetList"
	_, err = builderDao.WriteString("type " + util.Marshal(table.Name) + "Dao" + " interface {\n")
	_, err = builderDao.WriteString("\t" + "// todo: add your method and delete this line reference this demo\n")
	_, err = builderDao.WriteString("\t" + funName + "() (*[]" + util.Marshal(table.Name) + ",error)" + "\n")
	_, err = builderDao.WriteString("}" + "\n")
	//
	defaultName := "default" + util.Marshal(table.Name) + "Dao"
	_, err = builderDao.WriteString("type " + defaultName + " struct {\n")
	_, err = builderDao.WriteString("  engine *gorm.DB\n")
	_, err = builderDao.WriteString("}\n")
	//
	tableName := util.Marshal(table.Name)
	tableNameDao := util.Marshal(table.Name) + "Dao"
	_, err = builderDao.WriteString("func " + "New" + tableNameDao + " (engine *gorm.DB) *" + defaultName + " {\n")
	//内部
	_, err = builderDao.WriteString("\t" + "return " + "&" + defaultName + " {\n")
	_, err = builderDao.WriteString("\t" + "\t" + "engine: engine,\n")
	_, err = builderDao.WriteString("\t" + "}\n")
	_, err = builderDao.WriteString("}\n" + "\n")

	_, err = builderDao.WriteString("func (m *" + util.Marshal(table.Name) + ") TableName() string { \n    return \"" + table.Name + "\"\n}")
	//换行
	_, err = builderDao.WriteString("\n")
	//方法
	_, err = builderDao.WriteString("// todo: add your method and delete this line reference this demo\n")
	_, err = builderDao.WriteString("func (d " + defaultName + ")" + funName + "()" + " (*[]" + tableName + ", error) {\n")
	_, err = builderDao.WriteString("\t" + "s := new([]" + tableName + ")\n")
	_, err = builderDao.WriteString("\t" + "var err error" + "\n")
	_, err = builderDao.WriteString("\t" + "//这里写逻辑" + "\n" + "\n")
	_, err = builderDao.WriteString("\t" + "return s,err" + "\n")
	_, err = builderDao.WriteString("}\n" + "\n")

	fileNameDao := s.svcCtx.Config.GenerateModel.SavePath + table.Name + "dao.go"
	if nil != err {
		return err
	}
	util.MakeFile(s.svcCtx.Config.GenerateModel.SavePath, fileNameDao, builderDao.String(), s.svcCtx.Config.GenerateModel.ModelCover)
	return nil
}
